﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static UserControlLib.Enums.PopWindowEnum;
using UserControlLib.Enums;

namespace UserControlLib
{
    /// <summary>
    /// SMessageBox.xaml 的交互逻辑
    /// </summary>
    public partial class SMessageBox : Window
    {
        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(IconType), typeof(SMessageBox), new PropertyMetadata(IconType.Info, OnIconChanged));

        public static readonly DependencyProperty ButtonTypeProperty =
            DependencyProperty.Register("ButtonType", typeof(ButtonType), typeof(SMessageBox), new PropertyMetadata(ButtonType.Ok, OnButtonTypeChanged));

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(SMessageBox), new PropertyMetadata(default(string)));

        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(string), typeof(SMessageBox), new PropertyMetadata(default(string)));

        public IconType Icon
        {
            get => (IconType)GetValue(IconProperty);
            set => SetValue(IconProperty, value);
        }

        public ButtonType ButtonType
        {
            get => (ButtonType)GetValue(ButtonTypeProperty);
            set => SetValue(ButtonTypeProperty, value);
        }

        public string Title
        {
            get => (string)GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }

        public string Content
        {
            get => (string)GetValue(ContentProperty);
            set => SetValue(ContentProperty, value);
        }

        public SMessageBox()
        {
            InitializeComponent();
            UpdateButtons();
            UpdateIcon();
        }

        public SMessageBox(IconType icon, ButtonType buttonType, string title, string content)
            : this()
        {
            Icon = icon;
            ButtonType = buttonType;
            Title = title;
            Content = content;
        }

        private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as SMessageBox;
            if (control != null)
            {
                control.UpdateIcon();
            }
        }

        private static void OnButtonTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as SMessageBox;
            if (control != null)
            {
                control.UpdateButtons();
            }
        }

        private void UpdateIcon()
        {
            switch (Icon)
            {
                case IconType.Info:
                    IconImage.Source = new BitmapImage(new Uri("pack://application:,,,/UserControlLib;component/Assets/Images/info_icon.png"));
                    break;
                case IconType.Warning:
                    IconImage.Source = new BitmapImage(new Uri("pack://application:,,,/UserControlLib;component/Assets/Images/warning_icon.png"));
                    break;
                case IconType.Error:
                    IconImage.Source = new BitmapImage(new Uri("pack://application:,,,/UserControlLib;component/Assets/Images/error_icon.png"));
                    break;
            }
        }

        private void UpdateButtons()
        {
            // 按钮状态复位
            btn_OK.Visibility = Visibility.Visible;
            btn_OK.Margin = new Thickness(0, 0, 0, 0);

            switch (ButtonType)
            {
                case ButtonType.Ok:
                    btn_OK.Visibility = Visibility.Visible;
                    btn_Canel.Visibility = Visibility.Collapsed;
                    if (btn_Canel.Visibility == Visibility.Collapsed)
                    {
                        btn_OK.Margin = new Thickness(0, 0, 80, 0); // 调整确定按钮的间距
                    }
                    break;
                case ButtonType.OkCancel:
                    btn_OK.Visibility = Visibility.Visible;
                    btn_Canel.Visibility = Visibility.Visible;
                    break;
            }
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                this.DragMove();
            }
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
        /// <summary>
        /// 窗体加载设定窗体大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Width = 300;
            this.Height = 200;
        }
    }
}
